home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Technology Seed / ADC Seed CD - July 1999.toast / ColorSync / ColorSync 2.6 Mac SDK / Sample Code / DropShell / DropShell.c next >
Encoding:
C/C++ Source or Header  |  1999-04-12  |  7.5 KB  |  332 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DropShell.c
  5. **
  6. **   Description:    Main application code for the QuickShell
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **
  18. *******************************************************************************
  19. **                      R E V I S I O N   H I S T O R Y
  20. *******************************************************************************
  21. **
  22. **      Date        Author    Description
  23. **    ---------    ------    ---------------------------------------------
  24. **    23 Jun    94    LDR        Implemented support for disk insertion events
  25. **    02 Feb    94    LDR        Updated for Final SDK & CodeWarrior a2
  26. **                        Removed the ResumeProc as per new Apple recommendations
  27. **    11 Dec 93    SCS        Universal Headers/UPPs (Phoenix 68k/PPC & PPCC)
  28. **                        Skipped System 6 compatible rev of DropShell source
  29. **    09 Dec 91    LDR        Added support for new "Select File…" menu item
  30. **                        Quit now sends AEVT to self to be politically correct
  31. **                        Added support for the new gSplashScreen
  32. **    24 Nov 91    LDR        Added support for the Apple Menu (duh!)
  33. **    29 Oct 91    SCS        Changes for THINK C 5
  34. **    28 Oct 91    LDR        Officially renamed DropShell (from QuickShell)
  35. **                        Added a bunch of comments for clarification
  36. **    06 Oct 91    MTC        Converted to MPW C
  37. **    09 Apr 91    LDR        Added to Projector
  38. **
  39. ******************************************************************************/
  40.  
  41. #include <Devices.h>
  42. #include <Events.h>
  43. #include <Dialogs.h>
  44. #include <DiskInit.h>
  45. #include <Errors.h>
  46. #include <Files.h>
  47. #include <Fonts.h>
  48. #include <Memory.h>
  49. #include <Menus.h>
  50. #include <StandardFile.h>
  51. #include <TextEdit.h>
  52. #include <Types.h>
  53. #include <Windows.h>
  54.  
  55. #include "DSGlobals.h"
  56. #include "DSUserProcs.h"
  57. #include "DSAppleEvents.h"
  58. #include "DropShell.h"
  59.  
  60.  
  61.  
  62. Boolean        gDone, gOApped, gHasAppleEvents, gWasEvent;
  63. EventRecord    gEvent;
  64. MenuHandle    gAppleMenu, gFileMenu;
  65. WindowPtr    gSplashScreen;
  66. OSType*        gTypeList;
  67. short        gTypeListCount;
  68.  
  69. #ifdef MPW
  70. extern void _DataInit();    
  71. #endif
  72.  
  73.  
  74. #pragma segment Initialize
  75. void InitToolbox (void) 
  76. {
  77.  
  78. #ifdef MPW
  79.     UnloadSeg ((Ptr) _DataInit );
  80. #endif
  81.  
  82.     InitGraf ( &qd.thePort );
  83.     InitFonts ();
  84.     InitWindows ();
  85.     InitMenus ();
  86.     TEInit ();
  87.     InitDialogs (0L);        // use of ResumeProcs no longer approved by Apple
  88.     InitCursor ();
  89.     FlushEvents ( everyEvent, 0 );
  90.     
  91.     // how about some memory fun! Two should be enough!
  92.     MoreMasters ();
  93.     MoreMasters ();
  94. }
  95.  
  96. typedef struct openResource
  97. {
  98.     OSType    appSignature;
  99.     short    reserved;
  100.     short    count;
  101.     OSType    fileTypes[1];    // variable length
  102. } openResourceRec, *openResourcePtr, **openResourceHdl;
  103.  
  104.  
  105. /*
  106.     Let's setup those global variables that the DropShell uses.
  107.     
  108.     If you add any globals for your own use,
  109.     init them in the InitUserGlobals routine in DSUserProcs.c
  110. */
  111. #pragma segment Initialize
  112. Boolean InitGlobals (void) 
  113. {
  114.     long aLong;
  115.     openResourceHdl theOpenRes;
  116.     
  117.     gDone            = false;
  118.     gOApped            = false;    // probably not since users are supposed to DROP things!
  119.     gHasAppleEvents    = Gestalt ( gestaltAppleEventsAttr, &aLong ) == noErr;
  120.     gSplashScreen    = NULL;
  121.     
  122.     theOpenRes = (openResourceHdl)Get1IndResource( 'open', 1);
  123.     if( theOpenRes == 0)
  124.     {
  125.         gTypeListCount = -1;
  126.         gTypeList = nil;
  127.     }
  128.     else
  129.     {
  130.         Size byteCount;
  131.         
  132.         gTypeListCount = (**theOpenRes).count;
  133.         byteCount = gTypeListCount*sizeof(OSType);
  134.         gTypeList = (OSType*)NewPtr(byteCount);
  135.         HLock( (Handle)theOpenRes );
  136.         BlockMove((Ptr)((**theOpenRes).fileTypes), (Ptr)gTypeList, byteCount);
  137.         ReleaseResource( (Handle)theOpenRes);
  138.     }
  139.     
  140.     return(InitUserGlobals());    // call the user proc
  141. }
  142.  
  143. /*
  144.     Again, nothing fancy.  Just setting up the menus.
  145.     
  146.     If you add any menus to your DropBox - insert them here!
  147. */
  148. #pragma segment Initialize
  149. void SetUpMenus (void)
  150. {
  151.  
  152.     gAppleMenu = GetMenu ( kAppleNum );
  153.     AppendResMenu ( gAppleMenu, 'DRVR' );
  154.     InsertMenu ( gAppleMenu, 0 );
  155.  
  156.     gFileMenu = GetMenu ( kFileNum );
  157.     InsertMenu ( gFileMenu, 0 );
  158.     DrawMenuBar ();
  159. }
  160.  
  161. /*
  162.     This routine is called during startup to display a splash screen.
  163.     
  164.     This was recommend by the Blue Team HI person, John Sullivan, who
  165.     feels that all apps should display something so that users can easily
  166.     tell what is running, and be able to switch by clicking.  Thanks John!
  167. */
  168. #pragma segment Initialize
  169. void InstallSplashScreen(void) 
  170. {
  171.     #define windowPicID    128
  172.  
  173.     PicHandle    picH;
  174.  
  175.     if (!gSplashScreen)  // show the splash screen window
  176.     {
  177.         picH = GetPicture(windowPicID);
  178.         if (picH) {
  179.             gSplashScreen = GetNewWindow(windowPicID, NULL, (WindowPtr)-1L);
  180.             if (gSplashScreen)
  181.             {
  182.                 SetWindowPic(gSplashScreen, picH);
  183.                 // Don't show it here, since we only want to it for oapp launches!
  184.                 // ShowWindow(gSplashScreen);
  185.             }
  186.         }
  187.     }
  188. }
  189.  
  190.  
  191. /*    --------------- Standard Event Handling routines ---------------------- */
  192. #pragma segment Main
  193. void ShowAbout ( void )
  194. {
  195.     (void) Alert ( 128, NULL );
  196. }
  197.  
  198.  
  199. #pragma segment Main
  200. void DoMenu ( long retVal )
  201. {
  202.     short    menuID, itemID;
  203.     Str255    itemStr;
  204.  
  205.     menuID = HiWord ( retVal );
  206.     itemID = LoWord ( retVal );
  207.     
  208.     switch ( menuID )
  209.     {
  210.         case kAppleNum:
  211.             if ( itemID == 1 )
  212.                 ShowAbout();        // Show the about box
  213.             else
  214.             {
  215.                 GetMenuItemText(GetMenuHandle(kAppleNum), itemID, itemStr);
  216.                 OpenDeskAcc(itemStr);
  217.             }
  218.             break;
  219.             
  220.         case kFileNum:
  221.             if ( itemID == 1 )
  222.             {
  223.                 StandardFileReply    stdReply;
  224.                 StandardGetFile(NULL, gTypeListCount, gTypeList, &stdReply);
  225.                 if (stdReply.sfGood)                    // user did not cancel
  226.                     SendODOCToSelf(&stdReply.sfFile);    // so send me an event!
  227.             }
  228.             else
  229.                 SendQuitToSelf();    // send self a 'quit' event
  230.             break;
  231.         
  232.         default:
  233.             break;
  234.     }
  235.     HiliteMenu(0);        // turn it off!
  236. }
  237.  
  238.  
  239. #pragma segment Main
  240. void DoMouseDown ( EventRecord *curEvent )
  241. {
  242.     WindowPtr    whichWindow;
  243.     short        whichPart;
  244.  
  245.     whichPart = FindWindow ( curEvent->where, &whichWindow );
  246.     switch ( whichPart )
  247.     {
  248.         case inMenuBar:
  249.             DoMenu( MenuSelect ( curEvent->where ));
  250.             break;
  251.         
  252.         case inSysWindow:
  253.             SystemClick( curEvent, whichWindow );
  254.             break;
  255.         
  256.         case inDrag:
  257.             {
  258.                 Rect    boundsRect = (*GetGrayRgn())->rgnBBox;
  259.                 DragWindow( whichWindow, curEvent->where, &boundsRect );
  260.             }
  261.         default:
  262.             break;
  263.         }
  264.     }
  265.  
  266.  
  267. #pragma segment Main
  268. void DoKeyDown ( EventRecord *curEvent )
  269. {
  270.     if ( curEvent->modifiers & cmdKey )
  271.         DoMenu( MenuKey ((char) curEvent->message & charCodeMask ));
  272. }
  273.  
  274.  
  275.  
  276. #pragma segment Main
  277. void main ( void ) 
  278. {
  279.  
  280.     InitToolbox ();
  281.     if ( InitGlobals () )    // if we succeeding in initting self
  282.     {
  283.         if ( !gHasAppleEvents )
  284.             ErrorAlert ( kErrStringID, kCantRunErr, 0 );
  285.         else
  286.         {
  287.             InitAEVTStuff ();
  288.             SetUpMenus ();
  289.             InstallSplashScreen ();
  290.             
  291.             while ( !gDone )
  292.             {
  293.                 gWasEvent = WaitNextEvent ( everyEvent, &gEvent, 0, NULL );
  294.                 if ( gWasEvent )
  295.                 {
  296.                     switch ( gEvent.what )
  297.                     {
  298.                         case kHighLevelEvent:
  299.                             DoHighLevelEvent ( &gEvent );
  300.                             break;
  301.                             
  302.                         case mouseDown:
  303.                             DoMouseDown ( &gEvent );
  304.                             break;
  305.                             
  306.                         case keyDown:
  307.                         case autoKey:
  308.                             DoKeyDown ( &gEvent );
  309.                             break;
  310.  
  311.                         case diskEvt:
  312.                             if (HiWord(gEvent.message))
  313.                             {
  314.                                 Point diskInitPt;
  315.                                 
  316.                                 diskInitPt.v = diskInitPt.h = 100;
  317.                                 DILoad();
  318.                                 DIBadMount(diskInitPt, gEvent.message);
  319.                                 DIUnload();
  320.                             }
  321.                             break;
  322.                             
  323.                         default:
  324.                             break;
  325.                     }
  326.                 }
  327.             }
  328.         }
  329.         DisposeUserGlobals();    // call the userproc to clean itself up
  330.     }
  331. }
  332.